package com.flame.shapeville;

import com.flame.shapeville.controller.SceneController;
import com.flame.shapeville.enums.SceneStyle;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;

/**
 * JavaFX application class responsible for initializing the primary stage and setting up global event handlers.
 * <p>
 * Sets window properties, loads scenes via SceneController, and provides debugging tools like mouse position tracing.
 *
 * @author Flame Dev Team
 * @version 1.0
 * @since 2025-04-29
 */
public class JFApplication extends Application {

    /**
     * Starts the JavaFX application.
     * <p>
     * Sets up the primary stage with fixed dimensions, title, style, and initializes the login scene.
     *
     * @param stage The primary stage for this application
     */
    @Override
    public void start(Stage stage) {
        // Set primary stage reference in the SceneController
        SceneController.setPrimaryStage(stage);

        // Set initial scene to LOGIN style
        SceneController.setSceneStyle(SceneStyle.LOGIN);

        // Configure window properties
        stage.setTitle("Shapeville");
        stage.setHeight(768);
        stage.setWidth(1280);
        stage.setResizable(false);
        stage.initStyle(StageStyle.UNIFIED);
        stage.show();

        // Enable mouse click tracing for debugging
        traceMouse(stage, false);
    }

    /**
     * Enables/disables mouse click tracing for debugging purposes.
     * <p>
     * On right-click, prints the mouse coordinates to the console.
     *
     * @param stage Stage to attach the event filter to
     * @param on    Whether to enable or disable the feature
     */
    private void traceMouse(Stage stage, Boolean on) {
        if (on) {
            stage.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
                if (event.getButton() == MouseButton.SECONDARY) {
                    System.out.printf("X = %.0f, Y = %.0f\n", event.getX(), event.getY());
                }
            });
        }
    }
}
